package com.biaoyoo.www.personapp.core;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.biaoyoo.www.personapp.R;
import com.biaoyoo.www.personapp.activity.BaseActivity;
import com.biaoyoo.www.personapp.widget.DividerItemDecoration;
import com.biaoyoo.www.personapp.widget.PullToRefreshRecycler;
import com.biaoyoo.www.personapp.widget.pull.ILayoutManager;
import com.biaoyoo.www.personapp.widget.pull.MyLinearLayoutManager;

import java.util.ArrayList;

/**
 * <pre>
 *     author : 40575
 *     e-mail : huyongxi2015@gmail.com
 *     time   : 2017/05/26
 *     desc   :
 *     version: 1.0
 * </pre>
 */
public abstract class BaseListDefineActivity<T> extends BaseActivity implements PullToRefreshRecycler.OnRecycleRefreshListener {

    protected ArrayList<T> mDatalist;
    protected PullToRefreshRecycler mPullRefreshRecycler;
    protected BaseListDefineAdapter mAdapter;

    @Override
    protected void loadViews() {
        setContentView(R.layout.activity_base_list_define,R.string.baselist_activity);
        mPullRefreshRecycler = (PullToRefreshRecycler) findViewById(R.id.pullto_refresh_recycler);
    }

    @Override
    protected void initDatas() {
        mPullRefreshRecycler.setManager(getLayoutManager());
        mPullRefreshRecycler.setmAdapter(mAdapter);
        mPullRefreshRecycler.setRecycleRefreshListener(this);
        mPullRefreshRecycler.addItemDecoration(getItemDivider());
        //设置监听事件
        mPullRefreshRecycler.setRecycleRefreshListener(this);
        mPullRefreshRecycler.setIsLoadMoreEnabled(true); //设置开启加载更多
    }

    private RecyclerView.ItemDecoration getItemDivider() {
        return new DividerItemDecoration(BaseListDefineActivity.this,R.drawable.list_item_divider);
    }

    protected ILayoutManager getLayoutManager(){
        return new MyLinearLayoutManager(getApplicationContext());
    }



    @Override
    public void onRresh(int action) {

    }

    public class BaseListDefineAdapter extends RecyclerView.Adapter<BaseDefineViewHolder>{

        private static final int VIEW_TYPE_FOOER = 10;
        private boolean isShowLoadMoreFooter;

        @Override
        public BaseDefineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            if(viewType == VIEW_TYPE_FOOER){
                return getLordMore(parent,viewType);
            }
            return getDefineViewHolder(parent,viewType);
        }

        @Override
        public void onBindViewHolder(BaseDefineViewHolder holder, int position) {
            holder.bindData(position);
        }

        @Override
        public int getItemCount() {
            return mDatalist!=null ? mDatalist.size() + (isShowLoadMoreFooter ? 1 : 0): 0;
        }

         public void setDatalist(ArrayList<T> datalist) {
             mDatalist = datalist;
         }

        public boolean isFooterView(int position) {
            return isShowLoadMoreFooter && position == getItemCount() - 1;
        }

        @Override
        public int getItemViewType(int position) {
            if (isShowLoadMoreFooter && position == getItemCount() - 1) {
                return VIEW_TYPE_FOOER;
            }
            return getItemType(position);
        }

        public void showLoadMoreFooter(boolean isShow) {
            this.isShowLoadMoreFooter = isShow;
            if (isShow) {
                notifyItemInserted(getItemCount());
            } else {
                notifyItemRemoved(getItemCount());
            }

        }

    }

    protected int getItemType(int position) {
        return 0;
    }

    private BaseDefineViewHolder getLordMore(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_list_item_foot, parent, false);
        return new BaseDefineViewHolder(view){

            @Override
            public void bindData(int position) {

            }
        };
    }

    protected abstract BaseDefineViewHolder getDefineViewHolder(ViewGroup parent, int viewType);

    public abstract class BaseDefineViewHolder extends RecyclerView.ViewHolder{

        public BaseDefineViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindData(int position);
    }
}
